package com.pearl.jacksoncore.demo.databind.ser;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.SerializerFactory;
import com.pearl.jacksoncore.demo.databind.custom.UserVO;

import java.time.LocalDateTime;

/**
 *
 * @author TD
 * @version 1.0
 * @date 2024/4/11
 */
public class BeanJsonSerializerTest {

    public static void main(String[] args) throws JsonProcessingException {

        // 注册 BeanSerializerModifier
        ObjectMapper objectMapper = new ObjectMapper();
        // 创建新的 SerializerFactory，并设置 BeanSerializerModifier
        SerializerFactory serializerFactory = objectMapper.getSerializerFactory().withSerializerModifier(new ModifyPasswordBeanSerializerModifier());
        // 需要重新设置进去，因为SerializerFactory 是不可变对象
        objectMapper.setSerializerFactory(serializerFactory);

/*        // 使用模块注册BeanSerializerModifier：
        SimpleModule simpleModule=new SimpleModule("myModule");
        simpleModule.setSerializerModifier(new ModifyPasswordBeanSerializerModifier());
        objectMapper.registerModule(simpleModule);// 注册模块*/




        UserVO userVO = new UserVO();
        userVO.setId(1699657986705854464L);
        userVO.setUsername("jack");
        //userVO.setPassword("123456");
        //userVO.setCreateTime(LocalDateTime.now());
/*        List<String> roleList=new ArrayList<>();
        roleList.add("管理员");
        userVO.setRoleList(roleList);
        List<Org> orgList=new ArrayList<>();
        Org org=new Org();
        org.setOrgName("长沙大华分公司");
        orgList.add(org);
        userVO.setOrgList(orgList);*/
        String userVoJson = objectMapper.writeValueAsString(userVO);
        System.out.println(userVoJson);


        OrgInfo orgInfo = new OrgInfo();
/*
        OrgInfo orgInfo=new OrgInfo();
        orgInfo.setName("大华科技");
        orgInfo.setCode(123456);
        List<String> children=new ArrayList<>();
        children.add("长沙大华分公司");
        orgInfo.setChildren(children);
        UserInfo userInfo=new UserInfo();
        userInfo.setName("张三");
        orgInfo.setOrgAdmin(userInfo);
*/


        String value = objectMapper.writeValueAsString(orgInfo);
        System.out.println(value);
    }
}
